package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientTransfer;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.IClientTransferService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    private IClientService clientService;
    @Autowired
    private IClientTransferService clientTransferService;

    @RequestMapping("")
    public String stealth() {
        return "/client/list";
    }

    @RequestMapping("officialindex")
    public String official() {
        return "/client/official";
    }

    @RequestMapping("clientsourceindex")
    public String clientsource() {
        return "/client/clientsource";
    }


    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("client:list")
    @PermissionName("客户数据")
    private PageResult list(ClientQueryObject qo) {
        qo.setState(Client.STATE_LATENT);
        PageResult y = clientService.query(qo);
        return clientService.query(qo);
    }

    @RequiresPermissions("client:querys")
    @PermissionName("客户查询")
    @RequestMapping("querys")
    @ResponseBody
    private PageResult querys(ClientQueryObject qo) {
        return clientService.query(qo);
    }

    //===============查询所有的客户(分组查询用到)==================
    @RequestMapping("queryClients")
    @ResponseBody
    private List<Client> queryClients() {
        return clientService.selectAll();
    }

    @RequestMapping("official")
    @ResponseBody
    private PageResult official(ClientQueryObject qo) {
        qo.setState(Client.STATE_OFFI);
        System.out.println(qo);
        return clientService.query(qo);
    }

    @RequestMapping("clientsource")
    @ResponseBody
    private PageResult clientsource(ClientQueryObject qo) {
        qo.setState(Client.STATE_SOURCE);
        System.out.println(qo);
        return clientService.query(qo);
    }

    @RequiresPermissions("client:save")
    @PermissionName("客户保存")
    @RequestMapping("save")
    @ResponseBody
    private JsonResult save(Client recort) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.insertBrand(recort);
        } catch (Exception e) {
            jsonResult.setMsg("保存失败");
        }
        return jsonResult;
    }
    @RequiresPermissions("client:update")
    @PermissionName("客户更新")
    @RequestMapping("update")
    @ResponseBody
    private JsonResult update(Client recort) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.updateByPrimaryKey(recort);
        } catch (Exception e) {
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @RequestMapping("absorb")
    @ResponseBody
    private JsonResult  absorb(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.absorb(id);
        } catch (Exception e) {
            jsonResult.setMsg("吸纳客户失败");
        }
        return jsonResult;
    }



    @RequestMapping("movesource")
    @ResponseBody
    private JsonResult  movesource(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.movesource(id);
        } catch (Exception e) {
            jsonResult.setMsg("将客户放入资源池失败");
        }
        return jsonResult;
    }

    @RequestMapping("tryexploit")
    @ResponseBody
    private JsonResult  tryexploit(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.tryexploit(id);
        } catch (Exception e) {
            jsonResult.setMsg("操作失败");
        }
        return jsonResult;
    }


    @RequestMapping("admovesource")
    @ResponseBody
    private JsonResult  admovesource(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.admovesource(id);
        } catch (Exception e) {
            jsonResult.setMsg("将客户放入资源池失败");
        }
        return jsonResult;
    }

    @RequestMapping("trunOverClient")
    @ResponseBody
    private JsonResult trunOverClient(ClientTransfer clientTransfer) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientTransferService.trunOverClient(clientTransfer);
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查询正式客户信息
     * 返回PageResult,可以分页
     * */
    @RequestMapping("queryFormalClient")
    @ResponseBody
    private PageResult queryFormalClient(ClientQueryObject qo) {
        return clientService.queryFormalClient(qo);
    }

    @RequestMapping("queryClientIdsStoraId")
    @ResponseBody
    private Client queryClientIdsStoraId(Long stoId) {
        return clientService.queryClientIdsStoraId(stoId);
    }


    @RequestMapping("exportforlatentxls")
    @ResponseBody
    public JsonResult exportforlatentxls() {
        JsonResult jsonResult = new JsonResult();
        // 文件名
        DateFormat sdf = new DateFormat("yyyy-MM-dd");
        WritableCellFormat wcfDF = new WritableCellFormat(sdf);
        String fileName = "潜在客户明细表" + ".xls";
        //查询到的数据存进来

        List<Client> clients = clientService.allLatentClient(Client.STATE_LATENT);

        try {
            // 1.创建excel文件
            WritableWorkbook book = Workbook.createWorkbook(new File("e:/" + fileName));
            //创建sheet
            WritableSheet sheet = book.createSheet("潜在客户明细表", 0);
            //创建单元格
            String[] heard = new String[]{"创建时间", "负责人", "客户", "职业", "客户来源", "薪资水平", "邮箱", "QQ", "电话", "身份证号码", "开发标识"};
            Label label = null;
            for (int i = 0; i < heard.length; i++) {
                label = new Label(i, 0, heard[i]);
                sheet.addCell(label);
            }
            /*DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
            WritableCellFormat wcfDF = new WritableCellFormat(df);
            DateTime labelDTF = new DateTime(1, 3, new Date(), wcfDF);
            ws.addCell(labelDTF);*/
            //迭代数据  行可变  列直接写死
            for (int i = 1; i < clients.size() + 1; i++) {
                DateTime labelDTF = new DateTime(0, i, clients.get(i - 1).getInputdate(), wcfDF);
                sheet.addCell(labelDTF);
                label = new Label(1, i, clients.get(i - 1).getPrincipal().getUsername());
                sheet.addCell(label);
                label = new Label(2, i, clients.get(i - 1).getClientname());
                sheet.addCell(label);
                label = new Label(3, i, clients.get(i - 1).getProfession());
                sheet.addCell(label);
                label = new Label(4, i, clients.get(i - 1).getSource());
                sheet.addCell(label);
                label = new Label(5, i, clients.get(i - 1).getSalary());
                sheet.addCell(label);
                label = new Label(6, i, clients.get(i - 1).getEmail());
                sheet.addCell(label);
                label = new Label(7, i, clients.get(i - 1).getQq());
                sheet.addCell(label);
                label = new Label(8, i, clients.get(i - 1).getTel());
                sheet.addCell(label);
                label = new Label(9, i, clients.get(i - 1).getIdcard());
                sheet.addCell(label);
                if (clients.get(i - 1).getDevelopsgin() == 0) {
                    label = new Label(10, i, "建议转入资源库");
                } else {
                    label = new Label(10, i, "可开发");
                }
                sheet.addCell(label);
            }
            // 6.写入excel并关闭
            book.write();
            book.close();
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());

        }
        return jsonResult;
    }


    @RequestMapping("exportForOffiXls")
    @ResponseBody
    public JsonResult exportForOffiXls(int state) {
        JsonResult jsonResult = new JsonResult();
        // 文件名
        DateFormat sdf = new DateFormat("yyyy-MM-dd");
        WritableCellFormat wcfDF = new WritableCellFormat(sdf);
        String prex = null;
        String[] heard = new String[]{"创建时间", "创建人", "负责人", "客户", "职业", "客户来源", "薪资水平", "邮箱", "QQ", "电话", "身份证号码"};
        ;
        List<Client> clients = new ArrayList<>();
        if (state == 1) {
            prex = "正式客户明细表";
            //查询到的数据存进来
            clients = clientService.allLatentClient(Client.STATE_OFFI);
        } else if (state == 2) {
            heard = new String[]{"创建时间", "创建人", "负责人", "客户", "职业", "客户来源", "薪资水平", "邮箱", "QQ", "电话", "身份证号码", "开发标识"};
            prex = "潜在客户明细表";
            clients = clientService.allLatentClient(Client.STATE_LATENT);
        } else if (state == 0) {
            prex = "资源池客户明细表";
            clients = clientService.allLatentClient(Client.STATE_SOURCE);
        } else if (state == 3) {
            prex = "开发计划客户明细表";
            clients = clientService.allLatentClient(Client.STATE_PLAN);
        } else {
            prex = "所有客户明细表";
            clients = clientService.selectAll();
        }
        try {
            String fileName = prex + ".xls";
            // 1.创建excel文件
            WritableWorkbook book = Workbook.createWorkbook(new File("d:/" + fileName));
            //创建sheet
            WritableSheet sheet = book.createSheet(prex, 0);
            //创建单元格
            Label label = null;
            for (int i = 0; i < heard.length; i++) {
                label = new Label(i, 0, heard[i]);
                sheet.addCell(label);
            }
            /*DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
            WritableCellFormat wcfDF = new WritableCellFormat(df);
            DateTime labelDTF = new DateTime(1, 3, new Date(), wcfDF);
            ws.addCell(labelDTF);*/
            //迭代数据  行可变  列直接写死
            for (int i = 1; i < clients.size() + 1; i++) {
                DateTime labelDTF = new DateTime(0, i, clients.get(i - 1).getInputdate(), wcfDF);
                sheet.addCell(labelDTF);
                label = new Label(1, i, clients.get(i - 1).getInputuser().getUsername());
                sheet.addCell(label);
                label = new Label(1, i, clients.get(i - 1).getPrincipal().getUsername());
                sheet.addCell(label);
                label = new Label(2, i, clients.get(i - 1).getClientname());
                sheet.addCell(label);
                label = new Label(3, i, clients.get(i - 1).getProfession());
                sheet.addCell(label);
                label = new Label(4, i, clients.get(i - 1).getSource());
                sheet.addCell(label);
                label = new Label(5, i, clients.get(i - 1).getSalary());
                sheet.addCell(label);
                label = new Label(6, i, clients.get(i - 1).getEmail());
                sheet.addCell(label);
                label = new Label(7, i, clients.get(i - 1).getQq());
                sheet.addCell(label);
                label = new Label(8, i, clients.get(i - 1).getTel());
                sheet.addCell(label);
                label = new Label(9, i, clients.get(i - 1).getIdcard());
                sheet.addCell(label);
                if (state == 2) {
                    if (clients.get(i - 1).getDevelopsgin() == 0) {
                        label = new Label(10, i, "建议转入资源池");
                    } else {
                        label = new Label(10, i, "可开发");
                    }
                    sheet.addCell(label);
                }

            }
            // 6.写入excel并关闭
            book.write();
            book.close();
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());

        }
        return jsonResult;
    }

    @RequestMapping("exportForAllXls")
    @ResponseBody
    public JsonResult exportForAllXls() {
        JsonResult jsonResult = new JsonResult();
        // 文件名
        DateFormat sdf = new DateFormat("yyyy-MM-dd");
        WritableCellFormat wcfDF = new WritableCellFormat(sdf);
        List<Client> clients = new ArrayList<>();
        //查询到的数据存进来
        String[] heard = new String[]{"创建时间", "创建人", "负责人", "客户", "职业", "客户来源", "薪资水平", "邮箱", "QQ", "电话", "身份证号码", "客户状态"};
        clients = clientService.selectAll();
        try {
            String fileName = "所有客户明细表" + ".xls";
            // 1.创建excel文件
            WritableWorkbook book = Workbook.createWorkbook(new File("d:/" + fileName));
            //创建sheet
            WritableSheet sheet = book.createSheet("所有客户明细表", 0);
            //创建单元格
            Label label = null;
            for (int i = 0; i < heard.length; i++) {
                label = new Label(i, 0, heard[i]);
                sheet.addCell(label);
            }
            /*DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
            WritableCellFormat wcfDF = new WritableCellFormat(df);
            DateTime labelDTF = new DateTime(1, 3, new Date(), wcfDF);
            ws.addCell(labelDTF);*/
            //迭代数据  行可变  列直接写死
            for (int i = 1; i < clients.size() + 1; i++) {
                DateTime labelDTF = new DateTime(0, i, clients.get(i - 1).getInputdate(), wcfDF);
                sheet.addCell(labelDTF);
                label = new Label(1, i, clients.get(i - 1).getInputuser().getUsername());
                sheet.addCell(label);
                label = new Label(2, i, clients.get(i - 1).getPrincipal().getUsername());
                sheet.addCell(label);
                label = new Label(3, i, clients.get(i - 1).getClientname());
                sheet.addCell(label);
                label = new Label(4, i, clients.get(i - 1).getProfession());
                sheet.addCell(label);
                label = new Label(5, i, clients.get(i - 1).getSource());
                sheet.addCell(label);
                label = new Label(6, i, clients.get(i - 1).getSalary());
                sheet.addCell(label);
                label = new Label(7, i, clients.get(i - 1).getEmail());
                sheet.addCell(label);
                label = new Label(8, i, clients.get(i - 1).getQq());
                sheet.addCell(label);
                label = new Label(9, i, clients.get(i - 1).getTel());
                sheet.addCell(label);
                label = new Label(10, i, clients.get(i - 1).getIdcard());
                sheet.addCell(label);

                if (clients.get(i - 1).getState() == 1) {
                    label = new Label(11, i, "正式客户");
                } else if (clients.get(i - 1).getState() == 2) {
                    label = new Label(11, i, "潜在客户");
                } else if (clients.get(i - 1).getState() == 3) {
                    label = new Label(11, i, "正在开发中");
                } else {
                    label = new Label(11, i, "资源池中");
                }
                sheet.addCell(label);
            }
            // 6.写入excel并关闭
            book.write();
            book.close();
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());

        }
        return jsonResult;
    }

}
